<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script>
        window.onload= function () {
            var obtns = document.getElementsByTagName('button');
            // for(var i = 0;i<obtns.length;i++){
            //     (function (i) {
	         //        obtns[i].onclick =function () {
		     //            alert(i)
	         //        }
            //     })(i)
            // }

	        for (let i = 0; i < obtns.length; i++) {
                obtns[i].onclick =function () {
                    alert(i)
                }
	        }
        }
    </script>
</head>
<body>
<button>1</button>
<button>2</button>
<button>3</button>
</body>
<script>

</script>
</html>